import { myRequest } from '../utils/request.js'; // 导入底层请求工具
export const postService = {
    /**
     * 创建一个新帖子
     * @param {object} postData - 帖子数据，应包含 title 和 content
     * @returns {Promise} - 返回一个 Promise 对象，成功时解析为后端返回的数据，失败时拒绝
     */
    createPost(postData) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: 'posts',
                method: 'POST',
                data: postData,
                success: (response) => {
                    console.log('Service: Post created successfully', response);
                    resolve(response);
                },
                error: (err) => {
                    console.error('Service: Failed to create post', err);
                    reject(err);
                }
            });
        });
    },

    /**
     * 获取所有帖子的列表
     * @returns {Promise}
     */
   getAllPosts(userId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `posts/${userId}`,
                method: 'GET',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        });
    },
    updatePostLikes(postId,userId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `posts/${postId}/${userId}`,
                method: 'PUT',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        })
    },
    createComment(commentData) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: 'comments',
                method: 'POST',
                data: commentData,
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        });
    },
    getCommentsByPostId(postId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `comments/${postId}`, // 匹配后端 Controller 的获取接口
                method: 'GET',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        });
    },
    createArticle(articleData) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: 'articles',
                method: 'POST',
                data: articleData,
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        });
    },
    getArticleById(articleId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `articles/${articleId}`, // 匹配后端 @GetMapping("/{id}")
                method: 'GET',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        });
    },
    getAllArticles(userId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `articles/${userId}`,
                method: 'GET',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        });
    },
    updateArticleLikes(articleId,userId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `articles/${articleId}/${userId}`,
                method: 'PUT',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        })
    },
    getHotList() {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `articles/hotList`,
                method: 'GET',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        })
    },
    getLiked(postId,userId) {
        return new Promise((resolve, reject) => {
            myRequest.request({
                url: `liked/${postId}/${userId}`,
                method: 'GET',
                success: (response) => {
                    resolve(response);
                },
                error: (err) => {
                    reject(err);
                }
            });
        })
    },
    getTodayPostArticleNum(userId,success){
        myRequest.request({
            url: `posts/todayNum/${userId}`,
            method: 'GET',
            data:null,
            success: success
        })
    }
};